from fontrender.converter.models import Font

import cairo
import pango
import pangocairo

class PyGTKRenderer():
    def __init__(self,font,data, *args, **kwargs):
        self.font = font
        self.size = data['size']
#        self.distance = float(data['distance'])
        self.text = data['message']
#        self.parent = super(FontRenderer,self)

    def image(self, surface, context, font="sans 14", position=None,
        color=None, box_width=None, alignment=pango.ALIGN_CENTER,
        line_spacing=None, letter_spacing=None, extra_kerning=None):
        if color is None:
            color = (0.0, 0.0, 0.0)
        context.set_source_rgb(*color)
        pc = pangocairo.CairoContext(context)
        layout = pc.create_layout()
        layout.set_text(self.text)
        layout.set_font_description(pango.FontDescription("%s %s" % ('Schluss\-Vignetten',self.size)))

        f = open("nicka.txt", "w+")
        f.write('font: %s\n' % layout.get_font_description().to_filename())
        f.close()

        if box_width: layout.set_width(box_width)
        layout.set_alignment(alignment)

        if line_spacing: layout.set_spacing(spacing)
        alist = pango.AttrList()

        if letter_spacing:
            alist.insert(pango.AttrLetterSpacing(letter_spacing, 0, len(self.text)))

        if extra_kerning:
            for pos, kern in extra_kerning.iteritems():
                alist.insert(pango.AttrLetterSpacing(kern, pos, pos+1))

        layout.set_attributes(alist)
        if position is None:
            width, height = surface.get_width(), surface.get_height()
            w, h = layout.get_pixel_size()

        position = (width/2.0 - w/2.0, height/2.0 - h/2.0)
        context.move_to(*position)
        pc.show_layout(layout)
